<?php
namespace app\api\controller;
class Software extends Common{
    /**
     * 获取用户账号
     * @param  integer $device_id [description]
     * @return [type]             [description]
     */
    function getSoft($device_id=null){
        $user=$this->user;
        $member_id=$user['id'];
        if(is_null($device_id)){
            $device_id=$user['device_id'];
        }
    	$rs=model($this->table)->readAll(['status'=>1,'member_id'=>$member_id,'device_id'=>$device_id],0,['order'=>'sort,id']);
    	if(!empty($rs)){
            foreach ($rs as $k => $v) {
                if(!empty($v['head_img'])){
                    $rs[$k]['head_img']=images($v['head_img']);
                }
            }
    		return json('',0,$rs);
    	}
    	return json('该手机号尚未导入社交账号',1);
    }
    /**
     * 用户从客户端更改微信昵称或者头像
     * @return [type] [description]
     */
    function edit(){
        $uid=$this->user['id'];
        $map=['member_id'=>$uid];
        $post=input('data/a',[]);
        
        $ids=[];
        $data=[];
        $model=model($this->table);
        foreach ($post as $k => $v) {
            if(is_string($v)){
               $v=json_decode($v,true); 
            }
            if(isset($v['id'])){
                $has=$model->read(array_merge($map,['id'=>$v['id']]),'id,media_id');

                if(empty($has)){
                    continue;
                }
                $item=[];
                if(isset($v['name'])){
                    $item['name']=$v['name'];
                }
                if(isset($v['head_img'])&&isset($v['media_id'])){
                    $item['media_id']=$v['media_id'];
                    $item['head_img']=$v['head_img'];
                    if($has['media_id']>0){
                        $ids[]=$has['media_id']; //头像媒体ID
                    }
                }
                if(!empty($item)){
                    $item['id']=$has['id'];
                    $data[]=$item;
                }
            }
        }

        if(!empty($data)){
            $edit=$model->edit($data);
            if($edit){
                $this->delMedia($ids);
                return json('更新成功');
            }
        }
        return json('数据不完整',1);
        
    }
    /**
     * 开启/禁用微信账号
     * @param  integer $soft_id 微信ID
     * @param  boolean $isClose 是否禁用
     * @return json
     */
    function close($soft_id=[]){
        $uid=$this->user['id'];
        if(is_string($soft_id)){
            $soft_id=json_decode($soft_id,true);
        }
        $map=[
            'member_id'=>$uid,
            'id'=>['id','in',$soft_id]
        ];
        model($this->table)->edit(['status'=>0],$map);
        return json('操作成功');
    }
}
